<template>
  <el-select
    v-model="id"
    size="small"
    :remote-method="selAll"
    remote
    filterable
    style="width: 100%"
    placeholder="请选择部门"
    clearable
    @focus="selAll"
  >
    <el-option
      v-for="item in list"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    />
  </el-select>
</template>

<script>
import { queryDepts } from '@/api/organ'

export default {
  name: 'DeptSelect',
  props: {
    value: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [],
      id: null
    }
  },
  watch: {
    'value': {
      handler(n, o) {
        if (n !== null && n !== '') {
          this.$set(this, 'id', this.value)
        } else {
          this.$set(this, 'id', '')
        }
      },
      deep: true,
      immediate: true
    },
    'id': {
      handler(n, o) {
        this.$emit('input', this.id)
      },
      immediate: false
    }
  },
  created() {
    this.selAll()
  },
  methods: {
    selAll() {
      queryDepts({}).then(response => {
        this.list = response.list
      }).catch(err => {
        this.$message.error(err.msg || '请求失败')
      })
    }
  }
}
</script>

<style scoped>
</style>
